﻿namespace SLClient.CustomerControls
{
    using SLClient.Extension;
    using SLClient.Infrastructure;
    using SLClient.ServiceAPI;
    using SLClient.ViewModels;
    using SLClient.Views;
    using System;
    using System.Collections.ObjectModel;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;

    public class PlayDataInfo : ViewModelBase
    {
        private PlayData _currentPlayData;
        private bool _isLoading;
        private PlayData _lastPlayData;
        public int lotteryCatagoryId;

        public PlayDataInfo()
        {
            this._isLoading = true;
            this._lastPlayData = new PlayData();
            this._currentPlayData = new PlayData();
        }

        public PlayDataInfo(int lotteryCatagoryId, string tag)
        {
            this._isLoading = true;
            this.lotteryCatagoryId = lotteryCatagoryId;
            this.PlayDatas = new ObservableCollection<PlayData>();
            this._lastPlayData = new PlayData();
            this._currentPlayData = new PlayData();
            this.ViwTopInfos = new ObservableCollection<ViwTopWinnerInfo>();
            this.Init(lotteryCatagoryId, tag);
        }

        public void Init(int lotteryCatagoryId, string tag)
        {
            ServiceClient serviceClient = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            serviceClient.GetTopTenWinInfosCompleted += delegate(object obj, GetTopTenWinInfosCompletedEventArgs arg)
            {
                if (!arg.Cancelled)
                {
                    this.ViwTopInfos.Clear();
                    foreach (ViwTopWinnerInfo info in arg.Result)
                    {
                        this.ViwTopInfos.Add(info);
                    }
                    new IsolatedStorageHelper<PlayDataInfo>().CreateSettingsInStorage(tag, this);
                    this.IsLoading = false;
                    base.Notify<bool>(() => this.IsLoading);
                    //this.Notify<bool>(Expression.Lambda<Func<bool>>(Expression.Property(Expression.Constant(this, typeof(PlayDataInfo)), (MethodInfo) methodof(PlayDataInfo.get_IsLoading)), new ParameterExpression[0]));
                    MainFrame.GetIncomeMessage();
                    if (MainpageViewModel.NewPlayInfos.Count != 0)
                    {
                        MainpageViewModel.NewOrders();
                    }
                }
            };
            serviceClient.GetLatestOpenLotteriesRecordsCompleted += delegate(object obj, GetLatestOpenLotteriesRecordsCompletedEventArgs arg)
            {
                this.PlayDatas.Clear();
                foreach (OpenLotteryInfo info in arg.Result)
                {
                    PlayData data2 = new PlayData
                    {
                        LotterCatagoryId = info.LotteryCatagoryId,
                        BonusNumber = info.OpenNum,
                        Qh = info.LotteryOpenId,
                        OpenTime = info.OpenTime
                    };
                    PlayData item = data2;
                    this.PlayDatas.Add(item);
                }
                if (this.PlayDatas.Count > 0)
                {
                    this.LastPlayData = this.PlayDatas[0];

                    if (Convert.ToInt32(this.LastPlayData.Qh) % 1000 != 120)
                    {
                        this.CurrentPlayData.Qh = (Convert.ToInt32(this.LastPlayData.Qh) + 1).ToString();
                    }
                    else
                    {
                        this.CurrentPlayData.Qh = ServerDateTime.Now.ToString("yyMMdd") + "001";
                    }

                    if (lotteryCatagoryId != 1)
                    {
                        if (this.LastPlayData.LotterCatagoryId == 2)
                        {
                            this.CurrentPlayData.OpenTime = this.LastPlayData.OpenTime.AddDays(1.0).AddSeconds(10.0);
                            this.CurrentPlayData.StartTime = this.CurrentPlayData.OpenTime.Date.AddHours(10.0).AddSeconds(10.0);
                            this.CurrentPlayData.EndTime = this.CurrentPlayData.OpenTime.AddMinutes(-30.0);
                        }
                        else if ((ServerDateTime.Now.Hour >= 10) && (ServerDateTime.Now.Hour < 0x16))
                        {
                            this.CurrentPlayData.OpenTime = this.LastPlayData.OpenTime.AddMinutes(10.0).AddSeconds(5.0);
                            this.CurrentPlayData.StartTime = this.LastPlayData.OpenTime.AddSeconds(5.0);
                            this.CurrentPlayData.EndTime = this.CurrentPlayData.OpenTime.AddMinutes(-1.0);
                        }
                        else if ((ServerDateTime.Now.Hour >= 2) && (ServerDateTime.Now.Hour < 10))
                        {
                            this.CurrentPlayData.OpenTime = this.LastPlayData.OpenTime.Date.AddHours(10.0).AddSeconds(5.0);
                            this.CurrentPlayData.StartTime = this.LastPlayData.OpenTime.Date.AddHours(2.0).AddSeconds(5.0);
                            this.CurrentPlayData.EndTime = this.CurrentPlayData.OpenTime.AddMinutes(-1.0);
                        }
                        else
                        {
                            this.CurrentPlayData.OpenTime = this.LastPlayData.OpenTime.AddMinutes(5.0).AddSeconds(5.0);
                            this.CurrentPlayData.StartTime = this.LastPlayData.OpenTime.AddSeconds(5.0);
                            this.CurrentPlayData.EndTime = this.CurrentPlayData.OpenTime.AddSeconds(-30.0);
                        }
                    }
                    else
                    {
                        switch (this.LastPlayData.OpenTime.DayOfWeek)
                        {
                            case DayOfWeek.Sunday:
                            case DayOfWeek.Tuesday:
                                this.CurrentPlayData.OpenTime = this.LastPlayData.OpenTime.AddDays(2.0).AddSeconds(10.0);
                                break;

                            case DayOfWeek.Thursday:
                                this.CurrentPlayData.OpenTime = this.LastPlayData.OpenTime.AddDays(3.0).AddSeconds(10.0);
                                break;
                        }
                        this.CurrentPlayData.StartTime = this.CurrentPlayData.OpenTime.Date.AddDays(-1.0).AddHours(10.0);
                        this.CurrentPlayData.EndTime = this.CurrentPlayData.OpenTime.AddHours(-1.0).AddMinutes(-30.0);
                    }
                    base.Notify<PlayData>(() => this.LastPlayData);
                    base.Notify<PlayData>(() => this.CurrentPlayData);
                    //this.Notify<PlayData>(Expression.Lambda<Func<PlayData>>(Expression.Property(Expression.Constant(this, typeof(PlayDataInfo)), (MethodInfo) methodof(PlayDataInfo.get_LastPlayData)), new ParameterExpression[0]));
                    //this.Notify<PlayData>(Expression.Lambda<Func<PlayData>>(Expression.Property(Expression.Constant(this, typeof(PlayDataInfo)), (MethodInfo) methodof(PlayDataInfo.get_CurrentPlayData)), new ParameterExpression[0]));
                    serviceClient.GetTopTenWinInfosAsync(lotteryCatagoryId, this.LastPlayData.Qh, tag);
                }
            };
            if (this.IsLoading)
            {
                serviceClient.GetLatestOpenLotteriesRecordsAsync(lotteryCatagoryId, 5);
            }
        }

        public PlayData CurrentPlayData
        {
            get
            {
                return this._currentPlayData;
            }
            set
            {
                this._currentPlayData = value;
                base.Notify<PlayData>(() => this.CurrentPlayData);
                //base.Notify<PlayData>(Expression.Lambda<Func<PlayData>>(Expression.Property(Expression.Constant(this, typeof(PlayDataInfo)), (MethodInfo) methodof(PlayDataInfo.get_CurrentPlayData)), new ParameterExpression[0]));
            }
        }

        public bool IsLoading
        {
            get
            {
                return this._isLoading;
            }
            set
            {
                this._isLoading = value;
                base.Notify<bool>(() => this.IsLoading);
                //base.Notify<bool>(Expression.Lambda<Func<bool>>(Expression.Property(Expression.Constant(this, typeof(PlayDataInfo)), (MethodInfo) methodof(PlayDataInfo.get_IsLoading)), new ParameterExpression[0]));
            }
        }

        public PlayData LastPlayData
        {
            get
            {
                return this._lastPlayData;
            }
            set
            {
                this._lastPlayData = value;
                base.Notify<PlayData>(() => this.LastPlayData);
                //base.Notify<PlayData>(Expression.Lambda<Func<PlayData>>(Expression.Property(Expression.Constant(this, typeof(PlayDataInfo)), (MethodInfo) methodof(PlayDataInfo.get_LastPlayData)), new ParameterExpression[0]));
            }
        }

        public ObservableCollection<PlayData> PlayDatas { get; set; }

        public ObservableCollection<ViwTopWinnerInfo> ViwTopInfos { get; set; }
    }
}

